package com.universal.schedule.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.universal.schedule.R;
import com.universal.schedule.enity.Diary;

import java.io.File;
import java.util.List;

public class DiaryAdapter extends RecyclerView.Adapter<DiaryAdapter.DiaryViewHolder> {
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 数据集合
     */
    private List<Diary> data;
    private MyItemClickListener mItemClickListener;

    public DiaryAdapter(List<Diary> data, Context context) {
        this.data = data;
        this.mContext = context;
    }

    @Override
    public DiaryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载item 布局文件
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.diary_item, parent, false);
        return new DiaryViewHolder(view,mItemClickListener);
    }

    @Override
    public void onBindViewHolder(DiaryViewHolder holder, int position) {
        //将数据设置到item上
        Diary diary = data.get(position);
        if(null!=diary&&!diary.getImageUrl().equals("")){
            Glide.with(mContext).load(new File(diary.getImageUrl())).into(holder.diaryImage);
        }
//        holder.diaryImage.setImageResource(diary.getImageUrl());
        holder.name.setText(diary.getMessage());
        holder.time.setText(diary.getTime());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class DiaryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        ImageView diaryImage;
        TextView name;
        TextView time;
        MyItemClickListener mListener;

        public DiaryViewHolder(View itemView,MyItemClickListener myItemClickListener) {
            super(itemView);
            diaryImage = (ImageView) itemView.findViewById(R.id.image_item);
            name = (TextView) itemView.findViewById(R.id.name_item);
            time = (TextView) itemView.findViewById(R.id.time_item);
            mListener=myItemClickListener;
            itemView.setOnClickListener(this);
            int width = ((Activity) diaryImage.getContext()).getWindowManager().getDefaultDisplay().getWidth();
            ViewGroup.LayoutParams params = diaryImage.getLayoutParams();
            //设置图片的相对于屏幕的宽高比
            params.width = width/2;
            params.height =  (int) (200 + Math.random() * 300) ;
            diaryImage.setLayoutParams(params);
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

    /**
     * 创建一个回调接口
     */
    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }

}
